// cvisionDlg.cpp : implementation file
//

#include "stdafx.h"
#include "cvision.h"
#include "cvisionDlg.h"

#include "highgui.h"
//#include "sobelVideoController.h"
#include "OpticalflowVideoController.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CVideoAnalysisDlg dialog




CVideoAnalysisDlg::CVideoAnalysisDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CVideoAnalysisDlg::IDD, pParent)
	, duration(0)
	, m_iFrameno(0)
	, m_fEnergy(0)
	, m_fEnergyDiff(0)
	, m_fMutualinfo(0)
	, m_fNMutualinfo(0)
	,canSize(false)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CVideoAnalysisDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT1, duration);
	// 	DDX_Text(pDX, IDC_EDIT_FRAME, m_iFrameno);
	// 	DDX_Text(pDX, IDC_EDIT_ENERGY2, m_fEnergy);
	// 	DDX_Text(pDX, IDC_EDIT_ENERGY, m_fEnergyDiff);
	// 	DDX_Text(pDX, IDC_EDIT_MI, m_fMutualinfo);
	// 	DDX_Text(pDX, IDC_EDIT_NMI, m_fNMutualinfo);
	DDX_Control(pDX, IDC_STATIC_FRAME, m_stcFrameno);
	DDX_Control(pDX, IDC_STATIC_ENERGY2, m_stcEnergy);
	DDX_Control(pDX, IDC_STATIC_ENERGY, m_stcEnergyDiff);
	DDX_Control(pDX, IDC_STATIC_MI, m_stcMutualInfo);
	DDX_Control(pDX, IDC_STATIC_NMI, m_stcNMutualInfo);
}

BEGIN_MESSAGE_MAP(CVideoAnalysisDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTON1, &CVideoAnalysisDlg::OnOpen)
	ON_BN_CLICKED(IDOK, &CVideoAnalysisDlg::OnBnClickedOk)
	ON_BN_CLICKED(IDCANCEL, &CVideoAnalysisDlg::OnBnClickedCancel)
	ON_BN_CLICKED(IDC_BUTTON2, &CVideoAnalysisDlg::OnProcess)
	ON_BN_CLICKED(IDC_BUTTON3, &CVideoAnalysisDlg::OnBnClickedButtonPause)
	ON_BN_CLICKED(IDC_BUTTON4, &CVideoAnalysisDlg::OnBnClickedButtonStop)
	ON_WM_TIMER()
	ON_WM_ERASEBKGND()
END_MESSAGE_MAP()


// CVideoAnalysisDlg message handlers

BOOL CVideoAnalysisDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
	//CWnd *wnd = 
// 	CRect Rect;
// 	GetDlgItem(IDC_STATIC_PLOT)->/*GetWindowRect(&Rect);*/GetClientRect(&Rect);
//  	Rect.top += 10;
// 	
//  	Rect.left += 340;
// 	Rect.right += 330;
// 
// 	m_Plot.m_leftaxis.setaxis(0.0, 1000.0);//set range
// 
// 	m_Plot.Create(WS_CHILD|WS_VISIBLE, Rect, this , 12000);
// 
// 	m_Plot.SetSerie(0, PS_SOLID, RGB(255, 0, 0), 0.0, 5000.0, "Energy");
// 
// 	m_Plot.SetLegend(0, PS_SOLID, RGB(255, 0, 0), "Energy");
// 
// 	m_Plot.m_bAutoScrollX = true;

    //cvNamedWindow("Result");

	//OpticalflowVideoController::getInstance();
// 		cvNamedWindow("Result");
// 	 	cvMoveWindow("Result", 300+ 400/*image->width*/, 100);
	image= 0;

	ctStartTime = CTime::GetCurrentTime();

	duration= 1000; // default value
	UpdateData(false); // display the content of the variable in Edit Control

	int iInstallresult;
	iInstallresult = SetTimer(1, 1000, NULL);
	if(iInstallresult==0)
	{
		MessageBox("fail to install the timer!");
	}
	canSize = true;
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CVideoAnalysisDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CVideoAnalysisDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CVideoAnalysisDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CVideoAnalysisDlg::OnOpen()
{
  CFileDialog dlg(TRUE, _T("*.avi"), NULL,
    OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_HIDEREADONLY,
    _T("video files (*.avi) |*.avi|All Files (*.*)|*.*||"),NULL);
 
  dlg.m_ofn.lpstrTitle= _T("Open Image");
 
  if (dlg.DoModal() == IDOK) {
 
    CString path= dlg.GetPathName();  // contain the selected filename

	//SobelVideoController::getInstance()->setFile(path); 
	OpticalflowVideoController::getInstance()->setFile(path);
	
  }
}

void CVideoAnalysisDlg::OnBnClickedOk()
{
	// TODO: Add your control notification handler code here
	//cvDestroyAllWindows();
	cvReleaseImage(&image);
	//SobelVideoController::getInstance()->destroy();
	OpticalflowVideoController::getInstance()->destroy();
	OnOK();
}

void CVideoAnalysisDlg::OnBnClickedCancel()
{
	// TODO: Add your control notification handler code here
	cvDestroyAllWindows();
	cvReleaseImage(&image);
	//SobelVideoController::getInstance()->destroy();
	OpticalflowVideoController::getInstance()->destroy();
	//cvDestroyAllWindows();
	OnCancel();
}

void CVideoAnalysisDlg::OnProcess()
{
// 	cvNamedWindow("Image (AviProcessor)");
// 	cvMoveWindow("Image (AviProcesor)", 200, 100);

	cvNamedWindow("Result");
	cvMoveWindow("Result", 300+ 400/*image->width*/, 100);
	UpdateData(true); // display the content of the variable in Edit Control
	//SobelVideoController::getInstance()->run();
	KillTimer(1);
	OpticalflowVideoController::getInstance()->pause(duration);
	SetTimer(1, duration, NULL);

	OpticalflowVideoController::getInstance()->run(&m_iFrameno, &m_fEnergy, &m_fEnergyDiff, &m_fMutualinfo, &m_fNMutualinfo);
}

void CVideoAnalysisDlg::OnBnClickedButtonPause()
{
	UpdateData(true);
	KillTimer(1);
	OpticalflowVideoController::getInstance()->pause(duration);
	SetTimer(1, duration, NULL);
}

void CVideoAnalysisDlg::OnBnClickedButtonStop()
{
	//SobelVideoController::getInstance()->stop();
	KillTimer(1);
	OpticalflowVideoController::getInstance()->stop();
}

void CVideoAnalysisDlg::OnTimer(UINT_PTR nIDEvent)
{
	//static DWORD dwStartTime = CTime::GetCurrentTime();
	// TODO: Add your message handler code here and/or call default
	
	//UpdateData(true); // display the content of the variable in Edit Control
	//static int framenotemp = -1;

	CString csEnergy;
	csEnergy.Format(_T("Energy: %f"), m_fEnergy);
	CString csEnergyDiff;
	csEnergyDiff.Format(_T("Energy Diff.: %f"), m_fEnergyDiff);
	CString csMutualInfo;
	csMutualInfo.Format(_T("Mutual Info.: %f"), m_fMutualinfo);
	CString csNMutualInfo;
	csNMutualInfo.Format(_T("Normalized MI: %f"), m_fNMutualinfo);
	CString csFrameno;
	csFrameno.Format(_T("Frame #: %d"), m_iFrameno);

	m_stcFrameno.SetWindowText(csFrameno.GetBuffer(0));
	m_stcEnergy.SetWindowText(csEnergy.GetBuffer(0));
	m_stcEnergyDiff.SetWindowText(csEnergyDiff.GetBuffer(0));
	m_stcMutualInfo.SetWindowText(csMutualInfo.GetBuffer(0));
	m_stcNMutualInfo.SetWindowText(csNMutualInfo.GetBuffer(0));

// 	if (framenotemp = m_iFrameno)
// 	{
// 		KillTimer(1);
// 	}

// 	static BOOL pros = {FALSE};
// 	if (!pros)
// 	{
// 		pros = TRUE;
// 		double y = (double)m_fEnergy;
// 		m_Plot.AddPoint(0, ctStartTime+CTimeSpan(m_iFrameno)/*CTime::GetCurrentTime()*/, y);
// 
// 		Invalidate();
// 		pros = FALSE;
// 
// 		//framenotemp = m_iFrameno;		
// 	}
	CDialog::OnTimer(nIDEvent);
}

BOOL CVideoAnalysisDlg::OnEraseBkgnd(CDC* pDC)
{
	// TODO: Add your message handler code here and/or call default

	return CDialog::OnEraseBkgnd(pDC);
}
